package com.eflow.engine.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.eflow.model.domain.EflowUser;
import com.eflow.model.e.BpmConst;
import com.eflow.model.e.SelectUserTypeEnum;
import com.eflow.model.flowable.model.UserTaskExtendElement;
import org.flowable.bpmn.model.Activity;
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.Process;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.util.ProcessDefinitionUtil;
import org.flowable.engine.repository.ProcessDefinition;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 任务审批人bean
 * @author hui se
 * @description
 * @create 2021-03-22 15:33
 **/

public class UserTaskService implements BpmConst {


    public String getUser(DelegateExecution execution){
        String activityId = execution.getCurrentActivityId();
        Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
        Activity userTask =(Activity) process.getFlowElement(activityId, true);
        UserTaskExtendElement element = UserTaskExtendElement.parse(userTask);
        //2未用户自选
        SelectUserTypeEnum setType = SelectUserTypeEnum.getEnumByCode(element.getSetType());
        if(setType==SelectUserTypeEnum.USER||setType==SelectUserTypeEnum.POINT){
            if(userTask.getLoopCharacteristics()!=null){
                Object variable = execution.getVariable(userTask.getLoopCharacteristics().getElementVariable());
                return variable.toString();
            }else {
                Object variable = execution.getVariable(activityId+"_"+VAR_TASK_USER_LIST);
                //获取审批人集合
                if(variable!=null){
                    List<String> userList = (List<String>) variable;
                    return userList.get(0);
                }
            }
        }else {
            if(userTask.getLoopCharacteristics()!=null){
                Object variable = execution.getVariable(userTask.getLoopCharacteristics().getElementVariable());
                return variable.toString();
            }else {
                //取流程设置的第一个人为审批人
                ProcessDefinition processDefinition = ProcessDefinitionUtil.getProcessDefinition(execution.getProcessDefinitionId());
                String loginUser = Authentication.getAuthenticatedUserId();
                Map<String, Object> variables = execution.getVariables();
                List<EflowUser> eflowUsers =element.parseUserList(loginUser, processDefinition, execution.getProcessInstanceId(),activityId, variables);
                if(CollectionUtil.isNotEmpty(eflowUsers)){
                    return eflowUsers.get(0).getUserId();
                }
            }
        }
        return null;
    }

    public List<String> getUserList(DelegateExecution execution){
        String activityId = execution.getCurrentActivityId();
        Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId());
        BaseElement userTask = process.getFlowElement(activityId, true);
        UserTaskExtendElement element = UserTaskExtendElement.parse(userTask);
        SelectUserTypeEnum setType = SelectUserTypeEnum.getEnumByCode(element.getSetType());
        if(setType==SelectUserTypeEnum.USER||setType==SelectUserTypeEnum.POINT){
            Object variable = execution.getVariable(activityId+"_"+VAR_TASK_USER_LIST);
            //获取审批人集合
            if(variable!=null){
                List<String> userList = (List<String>) variable;
                    return userList;
                }else {
                    return Collections.emptyList();
                }
        }else {
            ProcessDefinition processDefinition = ProcessDefinitionUtil.getProcessDefinition(execution.getProcessDefinitionId());
            String loginUser = Authentication.getAuthenticatedUserId();
            Map<String, Object> variables = execution.getVariables();
            List<EflowUser> eflowUsers = element.parseUserList(loginUser, processDefinition,execution.getProcessInstanceId(), activityId, variables);
            return eflowUsers.stream().map(EflowUser::getUserId).collect(Collectors.toList());
        }
    }


}
